from django.db import models
from django.contrib.auth import get_user_model

User = get_user_model()


class TimeAbstract(models.Model):
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    modify_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        abstract = True


class Dict(models.Model):
    """
    数据字典表
    """
    key = models.CharField(max_length=80, verbose_name='键')
    value = models.CharField(max_length=80, verbose_name='值')
    desc = models.CharField(max_length=255, blank=True, null=True, verbose_name='备注')
    pid = models.ForeignKey(
        'self', blank=True, null=True, on_delete=models.SET_NULL, related_name='child'
    )

    class Meta:
        verbose_name = '字典'
        verbose_name_plural = verbose_name


class Business(TimeAbstract):
    """
    业务类型表
    """
    name = models.CharField(max_length=50, verbose_name='业务名称')
    desc = models.CharField(max_length=255, blank=True, null=True, verbose_name='备注')

    class Meta:
        verbose_name = '业务'
        verbose_name_plural = verbose_name
        ordering = ['id']


class Label(TimeAbstract):
    """
    设备标签表
    """
    name = models.CharField(max_length=50, verbose_name='标签名')
    desc = models.CharField(max_length=255, blank=True, null=True, verbose_name='备注')

    class Meta:
        verbose_name = '标签'
        verbose_name_plural = verbose_name
        ordering = ['id']


class DeviceInfo(TimeAbstract):
    """
    设备信息表
    """
    business_id = models.ForeignKey(Business, null=True, blank=True, on_delete=models.SET_NULL, verbose_name="业务")
    hostname = models.CharField(max_length=50, verbose_name='主机名')
    ip = models.CharField(max_length=80, verbose_name='IP/域名')
    port = models.IntegerField(blank=True, default=0, verbose_name='端口')
    auth_type = models.CharField(max_length=30, default='', verbose_name='认证类型')
    auth_user = models.CharField(max_length=50, blank=True, default='', verbose_name='认证用户')
    auth_key = models.TextField(blank=True, default='', verbose_name='认证秘钥')
    auth_pwd = models.CharField(max_length=80, blank=True, default='', verbose_name='认证密码')
    os_type = models.CharField(max_length=50, blank=True, default='', verbose_name='操作系统类型')
    os_version = models.CharField(max_length=100, blank=True, default='', verbose_name='系统版本')
    device_type = models.CharField(max_length=50, blank=True, default='', verbose_name='设备类型')
    device_model = models.CharField(max_length=150, blank=True, default='', verbose_name='设备型号')
    task_number = models.IntegerField(blank=True, default=0, verbose_name='项目任务数')
    status = models.CharField(max_length=10, blank=True, default='', verbose_name='状态')
    desc = models.CharField(max_length=500, blank=True, null=True, verbose_name='备注')
    changed_by = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL)
    labels = models.ManyToManyField(Label, blank=True, verbose_name="标签")

    class Meta:
        verbose_name = '设备信息'
        verbose_name_plural = verbose_name
        ordering = ['id']

    @property
    def _history_user(self):
        return self.changed_by

    @_history_user.setter
    def _history_user(self, value):
        self.changed_by = value



